// "邻接表"表示法
const graph = {
    0: [1, 2],
    1: [2],
    2: [0, 3],
    3: [3]
}
// 图深度优先遍历示例：
const visited = new Set()
const dfs = n => {
    console.log(n)
    visited.add(n)
    graph[n].forEach(c => {
        if (!visited.has(c)) {
            dfs(c)
        }
    })
}
dfs(2)
console.log('----------------------')
// 图广度优先遍历示例：
const bfs = n => {
    let stack = [n]
    const bfsVisited = new Set([n])
    while (stack.length){
        const r = stack.shift()
        console.log(r)
        graph[r].forEach(c => {
            if (!bfsVisited.has(c)) {
                stack.push(c)
                bfsVisited.add(c)
            }
        })
    }
}
bfs(2)

